package com.android.lovely.ui.viewmodel

import androidx.lifecycle.viewModelScope
import com.android.lovely.api.service.systemService
import com.android.lovely.api.service.userService
import com.android.lovely.const.ChatChange
import com.android.lovely.data.PayData
import com.android.lovely.framework.BaseViewModel
import com.android.lovely.framework.launchWithLoading
import com.kotlin.extensions.bus.FlowBus
import com.kotlin.extensions.bus.post
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow

/**
 *
 * @author why
 * @date 2025/3/12 19:01
 */
class ChatViewModel: BaseViewModel() {

    private val _unread = MutableSharedFlow<Int>(extraBufferCapacity = 1)
    val unread: SharedFlow<Int> get() = _unread


    fun readAll() {
        viewModelScope.launchWithLoading {
            userService.readAll()
            FlowBus.post(ChatChange())
        }
    }

    fun getUnread() {
        viewModelScope.launchSafety() {
            systemService.getTabInfo()
            val count = systemService.getUnreadMsg().getOrDefault("unread_num", 0)
            _unread.emit(count)
        }
    }
}